<?php
App::uses('AppController', 'Controller');
/**
 * Contratos Controller
 *
 * @property Contrato $Contrato
 */
class ContratosController extends AppController {

	public $dbName 		= 'dbinmo';
	public $dbUserName 	= 'root';
	public $dbUserPass 	= '';
/**
 * admin_index method
 *
 * @return void
 */
	public function admin_index() {
		$this->layout='back_end';
		$this->set('title_for_layout', 'Listado de Contratos');
		$this->set('listTitle', 'Listado de contratos');       
		$this->Contrato->recursive = 0;
		$this->set('contratos', $this->paginate());
	}

/**
 * admin_view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_view($id = null) {
		$this->layout='back_end';
		$this->set('title_for_layout', 'Vista de Contrato');
		$this->set('listTitle', 'Vista de contrato');       
		$this->Contrato->id = $id;
		if (!$this->Contrato->exists()) {
			throw new NotFoundException(__('Invalid contrato'));
		}
		$this->set('contrato', $this->Contrato->read(null, $id));
	}

/**
 * admin_add method
 *
 * @return void
 */
	public function admin_add() {
	
		$db = NewADOConnection('mysql');
		$db->Connect('localhost', $this->dbUserName, $this->dbUserPass, $this->dbName);
		$db->execute("SET NAMES utf8");
	
	
		$this->layout='back_end';
		$this->set('title_for_layout', 'Agragar Contratos');
		$this->set('listTitle', 'Agregar contrato');       
		if ($this->request->is('post')) {
			$this->Contrato->create();
			//echo '<pre>'; var_dump($_POST); echo '</pre>'; die();

			if ($this->Contrato->save($this->request->data)) {
				foreach($_POST['clientes'] as $cliente){
					$contrato_id    = $this->Contrato->getInsertId();
					$relacion_id 	= $cliente['relacion'];
					$cliente_id 	= $cliente['cliente'];
					if ($relacion_id != "" && $cliente_id != ""){
						$insert_query = 'INSERT INTO contrato_clientes (contrato_id,cliente_id,tipo_cliente_id) 
										VALUES ('.$contrato_id.','.$cliente_id.','.$relacion_id.')';
						//echo $insert_query; die();
						$db->execute($insert_query);
					}					
				}
				
				$this->Session->setFlash(__('The contrato has been saved'));
				$this->redirect(array('action' => 'admin_index'));
				
			} else {
				$this->Session->setFlash(__('The contrato could not be saved. Please, try again.'));
			}
		}
		$clientes_query = 'SELECT id,nombre,apellido FROM clientes';
		$propiedades_query = 'SELECT * FROM propiedades';
		
		$users 			= $this->Contrato->User->find('list');
		$clientes 		= $db->getAll($clientes_query);
		$tipoContratos 	= $this->Contrato->TipoContrato->find('list');
		$propiedades 	= $this->Contrato->Propiedad->find('list');
		$this->set(compact('tipoCliente','clientes','users', 'tipoContratos', 'propiedades'));
		
		$query_tipos_cliente 	= 'SELECT * FROM tipo_clientes';
		$tipos_cliente			= $db->getAll($query_tipos_cliente);
		
		$this->set('tipos_cliente',$tipos_cliente);
	}

/**
 * admin_edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_edit($id = null) {
	
		$db = NewADOConnection('mysql');
		$db->Connect('localhost', $this->dbUserName, $this->dbUserPass, $this->dbName);
		$db->execute("SET NAMES utf8");
		
		
	
		$this->layout='back_end';
		$this->set('title_for_layout', 'Editar Contrato');
		$this->set('listTitle', 'Editar de contrato');       
		$this->Contrato->id = $id;
		if (!$this->Contrato->exists()) {
			throw new NotFoundException(__('Invalid contrato'));
		}
		
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Contrato->save($this->request->data)) {
			
			
				$delete_ralated_query = 'DELETE FROM contrato_clientes WHERE contrato_id = ' . $id;
				$db->execute($delete_ralated_query);
			
				foreach($_POST['clientes'] as $cliente){
					$contrato_id    = $id;
					$relacion_id 	= $cliente['relacion'];
					$cliente_id 	= $cliente['cliente'];
					if ($relacion_id != "" && $cliente_id != ""){
						$insert_query = 'INSERT INTO contrato_clientes (contrato_id,cliente_id,tipo_cliente_id) 
										VALUES ('.$contrato_id.','.$cliente_id.','.$relacion_id.')';
						//echo $insert_query; die();
						$db->execute($insert_query);
					}					
				}
			
			
				$this->Session->setFlash(__('The contrato has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The contrato could not be saved. Please, try again.'));
			}
		} else {
		
			$get_relations_query 	= 'SELECT * FROM contrato_clientes WHERE contrato_id = ' . $id;
			$clientes_query 		= 'SELECT id,nombre,apellido FROM clientes';
			$query_tipos_cliente 	= 'SELECT * FROM tipo_clientes';
			
			$tipos_cliente			= $db->getAll($query_tipos_cliente);
			$clientes 				= $db->getAll($clientes_query);
			$relaciones_clientes	= $db->getAll($get_relations_query);
			
			$this->request->data = $this->Contrato->read(null, $id);
		}
		$users = $this->Contrato->User->find('list');
		$tipoContratos = $this->Contrato->TipoContrato->find('list');
		$propiedades = $this->Contrato->Propiedad->find('list');
		$this->set(compact('clientes','tipos_cliente','relaciones_clientes','users', 'tipoContratos', 'propiedades'));
	}

/**
 * admin_delete method
 *
 * @throws MethodNotAllowedException
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_delete($id = null) {

	$db = NewADOConnection('mysql');
	$db->Connect('localhost', $this->dbUserName, $this->dbUserPass, $this->dbName);
	$db->execute("SET NAMES utf8");
	
	$delete_query = 'DELETE FROM contratos WHERE id = ' . $id;
	if ($db->execute($delete_query) ){
		$delete_ralated_query = 'DELETE FROM contrato_clientes WHERE contrato_id = ' . $id;
		$this->Session->setFlash(__('Contrato deleted'));
		$this->redirect(array('action' => 'index'));
		$this->render('admin_index');
	}
	$this->Session->setFlash(__('Contrato not deleted'));
	$this->redirect(array('action' => 'index'));

	/*
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->Contrato->id = $id;
		if (!$this->Contrato->exists()) {
			throw new NotFoundException(__('Invalid contrato'));
		}
		if ($this->Contrato->delete()) {
			$this->Session->setFlash(__('Contrato deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Contrato was not deleted'));
		$this->redirect(array('action' => 'index'));
	*/
	
	}
}
